<?php
// 导入数据操作类
require_once(dirname(dirname(__FILE__)) . '/libs/db.class.php');
class Login
{
    protected $db;
    function __construct()
    {   
        $this->db=new DB();
    }

    function index(){
        $output = ['errcode' => 0, 'msg' => 'success'];
        echo json_encode($output);
    }
    /*
        登录验证
    */ 
    function login()
    {
        $output = ['errcode' => 0, 'msg' => 'success'];
        if (!empty($_POST)) {
            $username = $_POST['username'];
            $password = md5($_POST['password']);
            $sql = "select id,username,password from admins where username='{$username}' and password='{$password}'";
            $user = $this->db->get_one($sql);
            if (empty($user)) {
                $output['errcode'] = 1;
                $output['msg'] = '用户名或密码错误';
            } else {
                $output['msg'] = '登录成功';
                $crypt = ['id' => $user['id'], 'username' => $user['username'],'timestamp'=>time()];
                // 作为登录凭证, 客户端需要存储在本地存储localStorage中
                $output['token'] = base64_encode(json_encode($crypt));
            }
        } else {
            $output['errcode'] = 2;
            $output['msg'] = '请使用post方式提交';
        }
        echo json_encode($output);
    }


}
